/**
 * 格式化日期
 * @param {Object} date
 * @param {Object} fmt
 */
export function formatDate(date, fmt) {
	try {
		if (!(date instanceof Date)) {
			try {
				date = date.replace(/-/g, "/")
				date = new Date(date)
			} catch (e) {
				return '-'
			}
		}
		if (fmt === undefined || fmt === '') {
			fmt = 'yyyy-MM-dd hh:mm:ss'
		}
		if (/(y+)/.test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
		}
		const o = {
			'M+': date.getMonth() + 1,
			'd+': date.getDate(),
			'h+': date.getHours(),
			'm+': date.getMinutes(),
			's+': date.getSeconds()
		}
		for (const k in o) {
			if (new RegExp(`(${k})`).test(fmt)) {
				const str = o[k] + ''
				fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : ('00' + str).substr(str.length))
			}
		}
		return fmt
	} catch (e) {
		console.log(e)
		return '-'
	}
}

/**
 * @Author : liu.q [916000612@qq.com]
 * @Description : 日期转换为xx前
 */
export function formatMsgTime(dateTime, type = 1) {
	if (!(dateTime instanceof Date)) {
		try {
			dateTime = dateTime.replace(/-/g, "/")
			dateTime = new Date(dateTime)
		} catch (e) {
			return '-'
		}
	}

	/*传入日期*/
	var timespan = dateTime.getTime()

	var year = dateTime.getFullYear();
	var month = dateTime.getMonth() + 1;
	var day = dateTime.getDate();
	var hour = dateTime.getHours();
	var minute = dateTime.getMinutes();
	var second = dateTime.getSeconds();
	var now = new Date();
	var now_new = now.getTime()

	var milliseconds = 0;
	var timeSpanStr;

	if (type === 1) {
		milliseconds = now_new - timespan;
	} else {
		milliseconds = timespan - now_new;
	}

	if (milliseconds <= 1000 * 60 * 1) {
		if (type === 1) {
			timeSpanStr = '刚刚';
		} else {
			timeSpanStr = '马上';
		}
	} else if (1000 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60) {
		if (type === 1) {
			timeSpanStr = Math.round((milliseconds / (1000 * 60))) + '分钟前';
		} else {
			timeSpanStr = Math.round((milliseconds / (1000 * 60))) + '分钟后';
		}
	} else if (1000 * 60 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24) {
		if (type === 1) {
			timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60)) + '小时前';
		} else {
			timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60)) + '小时后';
		}
	} else if (1000 * 60 * 60 * 24 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24 * 15) {
		if (type === 1) {
			timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60 * 24)) + '天前';
		} else {
			timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60 * 24)) + '天后';
		}
	} else if (milliseconds > 1000 * 60 * 60 * 24 * 15 && year == now.getFullYear()) {
		if (month < 10) {
			month = "0" + month
		}
		if (day < 10) {
			day = "0" + day
		}
		if (hour < 10) {
			hour = "0" + hour
		}
		if (minute < 10) {
			minute = "0" + minute
		}
		timeSpanStr = month + '-' + day + ' ' + hour + ':' + minute;
	} else {
		if (month < 10) {
			month = "0" + month
		}
		if (day < 10) {
			day = "0" + day
		}
		if (hour < 10) {
			hour = "0" + hour
		}
		if (minute < 10) {
			minute = "0" + minute
		}
		timeSpanStr = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
	}
	return timeSpanStr;
};

/* 秒转时分秒 */
export function formatSeconds(value) {
	let result = parseInt(value)
	let h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600);
	let m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 %
		60));
	let s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));

	let res = '';
	if (h !== '00') res += `${h}小时`;
	if (m !== '00') res += `${m}分`;
	res += `${s}秒`;
	return res;
}